using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;

using Geekees.App.Cashbook.Lib;
using Geekees.App.Cashbook.Lib.BLL;
using Geekees.App.Cashbook.Lib.Helper;

namespace Geekees.App.Cashbook.Web
{
	/// <summary>
	/// Summary description for Login.
	/// </summary>
	public class Login : System.Web.UI.Page
	{
		protected System.Web.UI.WebControls.TextBox txtUserName;
		protected System.Web.UI.WebControls.TextBox txtPwd;
		protected System.Web.UI.WebControls.CheckBox cbRememberMe;
		protected System.Web.UI.WebControls.Button btnLogin;
	
		public string RedirectUrl
		{
			get
			{
				object o = Request.QueryString["redirectUrl"];
				return o == null ? string.Empty : Request.QueryString["redirectUrl"];
			}
		}

		private void Page_Load(object sender, System.EventArgs e)
		{
			if(!IsPostBack)
			{
				if( Request.Cookies[Consts.COOKIE_NAME_REMEMBER_ME] != null )
				{
					HttpCookie loginCookie = Request.Cookies[Consts.COOKIE_NAME_REMEMBER_ME];

					string userName = loginCookie["username"] == null ? string.Empty : loginCookie["username"];
					string ticket = loginCookie["ticket"] == null ? string.Empty : loginCookie["ticket"];
					if( LoginManager.LoginUsingTicket( userName, ticket ) )
					{
						Response.Redirect( this.RedirectUrl == string.Empty ? "Cashbook.aspx" : this.RedirectUrl );
						return;
					}
				}
				
				LoginManager.Logout();
			}
		}

		#region Web Form Designer generated code
		override protected void OnInit(EventArgs e)
		{
			//
			// CODEGEN: This call is required by the ASP.NET Web Form Designer.
			//
			InitializeComponent();
			base.OnInit(e);
		}
		
		/// <summary>
		/// Required method for Designer support - do not modify
		/// the contents of this method with the code editor.
		/// </summary>
		private void InitializeComponent()
		{    
			this.btnLogin.Click += new System.EventHandler(this.btnLogin_Click);
			this.Load += new System.EventHandler(this.Page_Load);

		}
		#endregion

		private void btnLogin_Click(object sender, System.EventArgs e)
		{
			string userName = this.txtUserName.Text.Trim();
			string pwd = this.txtPwd.Text.Trim();
			if( userName == string.Empty )
			{
				FlashMessage.SetFlash( "User name can't be empty!", FlashMessage.FlashMessageType.Error );
				return;
			}

			if(  pwd == string.Empty )
			{
				FlashMessage.SetFlash( "Password can't be empty!", FlashMessage.FlashMessageType.Error );
				return;
			}

			string md5pwd = MD5Helper.GetMd5Sum( pwd );
			if( LoginManager.Login( userName, md5pwd ) )
			{
				CheckBox rm = this.cbRememberMe;
				if (rm.Checked)
				{
					string ticket = LoginManager.GetCurrentUser().Ticket;
					HttpCookie loginCookie = new HttpCookie( Consts.COOKIE_NAME_REMEMBER_ME );
					Response.Cookies.Remove( Consts.COOKIE_NAME_REMEMBER_ME );
					Response.Cookies.Add( loginCookie );
					loginCookie.Values.Add( "username", userName );
					loginCookie.Values.Add( "ticket", ticket );

					DateTime dtExpiry = DateTime.Now.AddDays(1000); //you can add years and months too here
					Response.Cookies[ Consts.COOKIE_NAME_REMEMBER_ME ].Expires = dtExpiry;
				}
				else
				{
					HttpCookie loginCookie = new HttpCookie( Consts.COOKIE_NAME_REMEMBER_ME );
					Response.Cookies.Remove( Consts.COOKIE_NAME_REMEMBER_ME );
					Response.Cookies.Add( loginCookie );
					loginCookie.Values.Add( "username", userName );
					loginCookie.Values.Add( "ticket", "" );
					DateTime dtExpiry = DateTime.Now.AddSeconds(1); //you can add years and months too here
					Response.Cookies[ Consts.COOKIE_NAME_REMEMBER_ME ].Expires = dtExpiry;
            
				}

				Response.Redirect( this.RedirectUrl == string.Empty ? "Cashbook.aspx" : this.RedirectUrl );
			}
			else
				FlashMessage.SetFlash( "Username and password do not match!", FlashMessage.FlashMessageType.Error );
		}
	}
}
